<?php

// +----------------------------------------------------------------------
// | UKAFU [ Aggregate payment ] Dalian Zhi Yi Technology Co., Ltd.
// | 支付/发卡/房卡/代理 系统
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2018 http://www.ukafu.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinyu126 QQ 765858558
// +----------------------------------------------------------------------

namespace think\response;

use think\Request;
use think\Response;
/**
 * Description of Cache
 *
 * @author Administrator
 */
class Cache extends Response{
    private function endWith($haystack, $needle) {   
	  $length = strlen($needle);  
      if($length == 0)
      {    
          return true;  
      }  
      return (substr($haystack, -$length) === $needle);
 }
    public function getContent() {
        $data = parent::getContent();
        if (200 == $this->code) {
            $rq = Request::instance();
            $url = $rq->url();
            if(!$this->endWith($url, '.html')){
                $url = $url.'/index.html';
            }
            $urls = explode('/', $url);
            $parent = '.';
            $count = count($urls);
            for ($i = 0; $i < $count - 1; $i++) {
                if ($urls[$i]) {
                    $parent = $parent . DIRECTORY_SEPARATOR . $urls[$i];
                    if (!file_exists($parent)) {
                        mkdir($parent);
                    }
                }
            }
            $parent = $parent . DIRECTORY_SEPARATOR . $urls[$count - 1];
            file_put_contents($parent, $data);
        }
        return $data;
    }
}
